package com.ruoyi.hqfinance.controller;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.hqfinance.domain.HqNetworkBillDetail;
import com.ruoyi.hqfinance.domain.vo.HqNetworkBillDetailVo;
import com.ruoyi.hqfinance.service.HqNetworkBillDetailService;
import com.ruoyi.logistic.domain.PageDate;
import com.ruoyi.logistic.domain.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/hq/billDetail")
@Api(tags = "总部财务-网点账单明细")
@EnableAsync
public class HqNetworkBillDetailController {
    @Autowired
    private HqNetworkBillDetailService hqNetworkBillDetailService;

    @ApiOperation("查全部")
    @GetMapping("/selectAll")
    public R selectAll(PageDate pageDate) {
        List<HqNetworkBillDetail> billDetailList = hqNetworkBillDetailService.selectAll(pageDate);
        if (billDetailList == null || billDetailList.size() == 0) {
            return R.fail("暂无数据");
        } else {
            return R.ok(PageInfo.info(billDetailList));
        }
    }

    @ApiOperation("全部导出excel(异步)")
    @GetMapping("/exportAsync")
    public R exportAsync(Integer pageNum,Integer pageSize, String path, String sheetName) {
        if (!path.contains(":")||!(path.endsWith(".xlsx")
                ||path.endsWith(".xls")
        )){
            return R.fail("设置的导出文件路径错误！！！");
        }

        hqNetworkBillDetailService.exportAsync(pageNum,pageSize,path,sheetName);
        return R.ok();
    }

    @ApiOperation("添加")
    @PostMapping("/insert")
    public R insert(@RequestBody HqNetworkBillDetail hqNetworkBillDetail) {
        Boolean isAdd = hqNetworkBillDetailService.insert(hqNetworkBillDetail);
        if (isAdd) {
            return R.ok("添加成功");
        } else {
            return R.fail("添加失败");
        }
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public R update(@RequestBody HqNetworkBillDetail hqNetworkBillDetail) {
        Boolean isUpdate = hqNetworkBillDetailService.update(hqNetworkBillDetail);
        if (isUpdate) {
            return R.ok("修改成功");
        } else {
            return R.fail("修改失败");
        }
    }

    @ApiOperation("删除")
    @PostMapping("/delete")
    public R delete(Integer id) {
        Boolean isDelete = hqNetworkBillDetailService.deleteById(id);
        if (isDelete) {
            return R.ok("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }

    @ApiOperation("搜索")
    @PostMapping("/search")
    public R search(@RequestBody HqNetworkBillDetailVo vo) {
        if (vo != null){
            String startTime = vo.getStartTime().replace("-", "");
            String endTime = vo.getEndTime().replace("-", "");
            System.out.println(startTime);
            System.out.println(endTime);
            vo.setStartTime(startTime);
            vo.setEndTime(endTime);
        }
        List<HqNetworkBillDetail> billDetailList = hqNetworkBillDetailService.search(vo);
        if (billDetailList == null || billDetailList.size() == 0) {
            return R.ok("暂无数据");
        } else {
            return R.ok(PageInfo.info(billDetailList));
        }
    }

    @ApiOperation("查询金额汇总")
    @PostMapping("/collectAmount")
    public R collectAmount(@RequestBody HqNetworkBillDetailVo vo) {
        if (vo != null){
            String startTime = vo.getStartTime().replace("-", "");
            String endTime = vo.getEndTime().replace("-", "");
            System.out.println(startTime);
            System.out.println(endTime);
            vo.setStartTime(startTime);
            vo.setEndTime(endTime);
        }
        return R.ok(hqNetworkBillDetailService.collectAmount(vo));
    }

}
